package com.crm.business.crm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.crm.business.crm.service.CrmInstrumentService;
import com.crm.business.crm.service.ICrmInstrumentSortService;
import com.crm.common.crm.entity.BO.CrmSearchParamsBO;
import com.crm.common.crm.entity.PO.BrokenLineDataParameter;
import com.crm.common.crm.entity.PO.CrmActivity;
import com.crm.common.crm.entity.VO.*;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.common.ka.core.feign.crm.entity.BiParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * CRM仪表盘 前端控制器
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/crmInstrument")
@Api(tags = "仪表盘相关接口")
public class CrmInstrumentController {

    @Autowired
    private ICrmInstrumentSortService sortService;

    @Autowired
    private CrmInstrumentService instrumentService;

    @PostMapping("/queryModelSort")
    @ApiOperation("查询模块排序")
    public Result<JSONObject> queryModelSort() {
        JSONObject object = sortService.queryModelSort();
        return Result.ok(object);
    }

    @PostMapping("/setModelSort")
    @ApiOperation("修改模块排序")
    public Result setModelSort(@RequestBody JSONObject jsonObject) {
        sortService.setModelSort(jsonObject);
        return R.ok();
    }

    @PostMapping("/queryBulletin")
    @ApiOperation("查询销售简报")
    public Result<JSONObject> queryBulletin(@RequestBody BiParams biParams) {
        JSONObject jsonObject = instrumentService.queryBulletin(biParams);
        return R.ok(jsonObject);
    }

    @PostMapping("/querySalesData")
    @ApiOperation("查询销售数据")
    public Result<DashBoardVO> querySalesData(@RequestBody BiParams biParams) {
        DashBoardVO jsonObject = null;
        if(BeanUtil.isEmpty(biParams.getDataForm())){
            biParams.setDataForm("涵品");
        }
        try {
            jsonObject = instrumentService.querySalesData(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryStatisticsCurve")
    @ApiOperation("查询统计折线图")
    public Result<JSONObject> queryStatisticsCurve(@RequestBody BrokenLineDataParameter brokenLineDataParameter) {
        JSONObject jsonObject = null;
        try {
            jsonObject = instrumentService.queryStatisticsCurve(brokenLineDataParameter);
        } catch (Exception e) {
            throw e;
        }
        return R.ok(jsonObject);
    }

    /**
     * 查询最近8期的数据信息
     * @param brokenLineDataParameter
     * @return
     */
    @PostMapping("/queryPeriodsStatisticsData")
    @ApiOperation("查询最近8期的数据信息")
    public Result<JSONObject> queryPeriodsStatisticsData(@RequestBody BrokenLineDataParameter brokenLineDataParameter) {
        JSONObject jsonObject = null;
        try {
            jsonObject = instrumentService.queryPeriodsStatisticsData(brokenLineDataParameter);
        } catch (Exception e) {
            throw e;
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryBulletinInfo")
    @ApiOperation("查询销售简报详情")
    public Result<BasePage<Map<String, Object>>> queryBulletinInfo(@RequestBody BiInstrumentParams biParams) {
        BasePage<Map<String, Object>> basePage = instrumentService.queryBulletinInfo(biParams);
        return R.ok(basePage);
    }

    @PostMapping("/forgottenCustomerCount")
    @ApiOperation("遗忘客户统计")
    public Result<JSONObject> forgottenCustomerCount(@RequestBody BiParams biParams) {
        JSONObject jsonObject = instrumentService.forgottenCustomerCount(biParams);
        return R.ok(jsonObject);
    }

    @PostMapping("/sellFunnel")
    @ApiOperation("销售漏斗")
    public Result<JSONObject> sellFunnel(@RequestBody BiParams biParams) {
        JSONObject jsonObject = instrumentService.sellFunnel(biParams);
        return R.ok(jsonObject);
    }

    @PostMapping("/sellFunnelBusinessList")
    @ApiOperation("销售漏斗商机状态列表")
    public Result<BasePage<Map<String, Object>>> sellFunnelBusinessList(@RequestBody CrmSearchParamsBO crmSearchParamsBO) {
        BasePage<Map<String, Object>> mapBasePage = instrumentService.sellFunnelBusinessList(crmSearchParamsBO);
        return R.ok(mapBasePage);
    }

    @PostMapping("/salesTrend")
    @ApiOperation("销售趋势")
    public Result<JSONObject> salesTrend(@RequestBody BiParams biParams) {
        JSONObject jsonObject = instrumentService.salesTrend(biParams);
        return R.ok(jsonObject);
    }

    @PostMapping("/queryDataInfo")
    @ApiOperation("数据汇总")
    public Result<JSONObject> queryDataInfo(@RequestBody BiParams biParams) {
        JSONObject jsonObject = instrumentService.queryDataInfo(biParams);
        return R.ok(jsonObject);
    }

    @PostMapping("/queryPerformance")
    @ApiOperation("业绩指标")
    public Result<JSONObject> queryPerformance(@RequestBody BiParams biParams) {
        JSONObject jsonObject = instrumentService.queryPerformance(biParams);
        return R.ok(jsonObject);
    }

    @PostMapping("/queryRecordList")
    @ApiOperation("查询跟进记录统计列表")
    public Result<BasePage<CrmActivity>> queryRecordList(@RequestBody BiParams biParams) {
        BasePage<CrmActivity> page = instrumentService.queryRecordList(biParams);
        return Result.ok(page);
    }

    @PostMapping("/queryRecordCount")
    @ApiOperation("查询销售简报的跟进记录统计")
    public Result<List<JSONObject>> queryRecordCount(@RequestBody BiParams biParams) {
        List<JSONObject> data = instrumentService.queryRecordCount(biParams);
        return Result.ok(data);
    }

    /**
     * 遗忘客户列表
     */
    @PostMapping("/forgottenCustomerPageList")
    @ApiOperation("遗忘客户列表")
    public Result<BasePage<Map<String, Object>>> forgottenCustomerPageList(@RequestBody BiParams biParams) {
        BasePage<Map<String, Object>> page = instrumentService.forgottenCustomerPageList(biParams);
        return Result.ok(page);
    }

    @PostMapping("/unContactCustomerPageList")
    @ApiOperation("未联系客户列表")
    public Result<BasePage<Map<String, Object>>> unContactCustomerPageList(@RequestBody BiParams biParams) {
        BasePage<Map<String, Object>> page = instrumentService.unContactCustomerPageList(biParams);
        return Result.ok(page);
    }

    @PostMapping("/queryMarketingDepartmentSalesData")
    @ApiOperation("查询市场部销售数据")
    public Result<MarketingDepartmentDashBoardVO> queryMarketingDepartmentSalesData(@RequestBody BiParams biParams) {
        MarketingDepartmentDashBoardVO jsonObject = null;
        if(BeanUtil.isEmpty(biParams.getDataForm())){
            biParams.setDataForm("涵品");
        }
        try {
            jsonObject = instrumentService.queryMarketingDepartmentSalesData(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryEverydayFormCount")
    @ApiOperation("查询每日表单数量")
    public Result<JSONObject> queryEverydayFormCount(@RequestBody BiParams biParams){
        JSONObject jsonObject = null;
        try {
            jsonObject = instrumentService.queryEverydayFormCount(biParams);
        } catch (Exception e) {
            throw e;
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryDataFromTransfer")
    @ApiOperation("查询渠道转化")
    public Result<JSONObject> queryDataFromTransfer(@RequestBody BiParams biParams){
        JSONObject jsonObject = null;
        try {
            jsonObject = instrumentService.queryDataFromTransfer(biParams);
        } catch (Exception e) {
            throw e;
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/querySalesDepartmentSalesData")
    @ApiOperation("查询销售部销售数据")
    public Result<SalesDepartmentDashBoardVO> querySalesDepartmentSalesData(@RequestBody BiParams biParams) {
        SalesDepartmentDashBoardVO jsonObject = null;
        if(BeanUtil.isEmpty(biParams.getDataForm())){
            biParams.setDataForm("涵品");
        }
        try {
            jsonObject = instrumentService.querySalesDepartmentSalesData(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/querySalesDepartmentDashboardData")
    @ApiOperation("查询销售部仪表盘数据")
    public Result<SalesDepartmentDashBoardDataVO> querySalesDepartmentDashboardData(@RequestBody BiParams biParams) {
        SalesDepartmentDashBoardDataVO jsonObject = null;
        if(BeanUtil.isEmpty(biParams.getDataForm())){
            biParams.setDataForm("涵品");
        }
        try {
            jsonObject = instrumentService.querySalesDepartmentDashboard(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryDaysDealCount")
    @ApiOperation("查询每日成交数量")
    public Result<JSONObject> queryDaysDealCount(@RequestBody BiParams biParams){
        JSONObject jsonObject = null;
        try {
            jsonObject = instrumentService.queryDaysDealCount(biParams);
        } catch (Exception e) {
            throw e;
        }
        return R.ok(jsonObject);
    }

    /**
     * 查看教学部销售数据
     * @param biParams
     * @return
     */
    @PostMapping("/queryTeachingDepartmentData")
    @ApiOperation("查询教学部销售数据")
    public Result<TeachingDepartmentDashBoardVO> queryTeachingDepartmentData(@RequestBody BiParams biParams){
        TeachingDepartmentDashBoardVO jsonObject = null;
        try {
            jsonObject = instrumentService.queryTeachingDepartmentData(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryTeachingDepartmentTableData")
    @ApiOperation("查询教学部表格数据")
    public Result<List<CrmContactsInstrumentTableData>> queryTeachingDepartmentTableData(@RequestBody BiParams biParams){
        List<CrmContactsInstrumentTableData> jsonObject = null;
        try {
            jsonObject = instrumentService.queryTeachingDepartmentTableData(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/queryStatDataForClassData")
    @ApiOperation("根据课程排期查询相关数据信息")
    public Result<SalesDepartmentDashBoardDataVO> queryStatDataForClassData(@RequestBody BiParams biParams){
        SalesDepartmentDashBoardDataVO jsonObject = null;
        if(BeanUtil.isEmpty(biParams.getDataForm())){
            biParams.setDataForm("涵品");
        }
        try {
            jsonObject = instrumentService.queryStatDataForClassData(biParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(jsonObject);
    }

    @PostMapping("/leadsStatis")
    @ApiOperation("线索总数")
    public Result<Map<String,Object>> leadsStatis(@RequestBody BiParams biParams){
        return R.ok(instrumentService.leadsStatis(biParams));
    }

    @PostMapping("/numStatis")
    @ApiOperation("总数统计")
    public Result<Map<String,Object>> numStatis(@RequestBody BiParams biParams){
        return R.ok(instrumentService.numStatis(biParams));
    }

    @PostMapping("/moneyStatis")
    @ApiOperation("金额统计")
    public Result<Map<String,Object>> moneyStatis(@RequestBody BiParams biParams){
        return R.ok(instrumentService.moneyStatis(biParams));
    }

    @PostMapping("/rankStatis")
    @ApiOperation("金额排行")
    public Result<List<JSONObject>> rankStatis(@RequestBody BiParams biParams){
        return R.ok(instrumentService.rankStatis(biParams));
    }
}
